<html>
<head>
<script src="https://unpkg.com/stackedit-js@1.0.7/docs/lib/stackedit.min.js"></script>
<script>
function getQueryString(name) {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    let r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    };
    return null;
}
async function fetchMD() {
	const el = document.querySelector('textarea');
    //const url = 'https://alist.reaky.top/Notes/QuickNote.md';
    const url = getQueryString('file');
	console.log(url);
    const filename = 'aaa';
	const response = await fetch(url, {
		method: "POST", // *GET, POST, PUT, DELETE, etc.
		mode: "cors", // no-cors, *cors, same-origin
		cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
		credentials: "same-origin", // include, *same-origin, omit
		headers: {
			//"Content-Type": "application/json",
			// 'Content-Type': 'application/x-www-form-urlencoded',
			'Access-Control-Allow-Origin': '*',
			'Origin': '*'
		}
    }).catch(function(error) {
          console.log(error);
    });
	const md = await response.text()
	el.value = md;

	const stackedit = new Stackedit();
	stackedit.openFile({
		name: "filename",
		content: {
			text: el.value
		}
	});
	stackedit.on('fileChange', (file) => {
		el.value = file.content.text;
	});
	stackedit.on('onChange', (file) => {
		el.value = file.content.text;
	});
}

document.addEventListener("DOMContentLoaded", () => {
    console.log("ready...");
	fetchMD();
});
</script>
</head>
<body>
<textarea></textarea>
</body>
</html>
